home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13757 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Beginner need help??????????????
  5. Date: 9 Apr 1996 22:02 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <9APR199622022758@erich.triumf.ca>
  9. References: <4kem82$5j3@dewey.csun.edu>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4kem82$5j3@dewey.csun.edu>, kc44097@csun.edu (chen) writes...
  14. >       I am a new comer of C language and I use Boland C++ 3.1 as
  15. >my compiler.These days I found some wierd things happens.I do not
  16. >-------- program 1 -------------------------
  17. >#include <stdio.h>
  18. >float answer;  
  19. >main ()
  20. >{
  21. >  answer = 1 / 3;
  22. >  printf("The value of 1/3 is %f\n",
  23. >                 answer);
  24. >  return(0);
  25. >}
  26. >????? The answer should be 0.333333 but compiler give me 0.000000 ??????
  27.  
  28. Since 1 and 3 are int constants, the division will be done as an integer
  29. operation - with a whole number result - no fractional part.  This integer
  30. result is then converted to a float and assigned to answer.
  31.  
  32. One way to make this work is to make one or both the operands into floats,
  33. thus:
  34.     answer = 1.0 / 3.0;
  35.  
  36.  
  37. >----------program 2----------------------------
  38. >#include <stdio.h>
  39. >float result;
  40. >main()
  41. >{
  42. >  result = 7.0 / 22.0;
  43. >  printf("The result is %d\n", result);
  44. >  return (0);
  45. >}
  46. >???? The answer should be 0.31818 but compiler give me 0 ????
  47.  
  48. If you lie to the compiler it _will_ get it's revenge!
  49.  
  50. By using "%d", you have told the compiler you are passing an int, but result is
  51. really a float.  The results of this are undefined - they depend on the format
  52. and byte order of the internal double (the float will be "promoted" to double
  53. to be passed to printf()) and int variables.
  54.  
  55. >----------program 3---------------------------
  56. >#include <stdio.h>
  57. >int   integer;
  58. >float floating;
  59. >main()
  60. >{
  61. >  floating = 7.0 / 22.0;
  62. >  integer = floating;
  63. >  printf("The value of integer is %f\n", integer);
  64. >  return (0);
  65. >}
  66. >???? The answer should be 0.31818 but compiler give me 0.000000 ?????
  67.  
  68. You lie to the compiler again, and again it takes revenge!
  69.  
  70. Even if you didn't lie, this would not give the expected result.  An int can
  71. only handle whole numbers.  When you do:
  72.     integer = floating;
  73. the program will assign the integer part of floating (which is 0) to integer.
  74.  
  75. You then tell printf() (with "%f") that you are passing a float, but actually
  76. pass an int - printf() will try to convert that int, probably plus a few more
  77. bytes from the stack, as if it was a double, and print the result.  Once again,
  78. the results are undefined, and rarely desireable.
  79.  
  80. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  81. Internet: bennett@triumf.ca         | of one another only when one can be
  82. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  83. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  84. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  85. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.